import { _decorator, AudioSource, Component, BoxCollider2D, Contact2DType, Collider2D } from 'cc';
import { Util } from '../Util';
import { Globals } from '../Globals';
import { getPlayerData, PlayerData } from '../Data/PlayerData';
import StorageManager from '../Data/StorageManager';

const { ccclass, property } = _decorator;

@ccclass('Player_Buttle')
export class Player_Buttle extends Component {

    // 伤害
    hurt: number = 50; //这里不算
    // 是否移动
    public isMoveing: boolean = true;
    // 阻力
    private _resistance = 0.1;

    private _audioSound: AudioSource;

    private boxCollider2D: BoxCollider2D;

    // 速度
    private _speed: number = 30;

    public get speed(): number {
        return this._speed;
    }

    public set speed(value: number) {
        this._speed = value;
    }

    // 方向
    private _moveDirection: number = 0;

    public get moveDirection(): number {
        return this._moveDirection;
    }

    public set moveDirection(value: number) {
        this._moveDirection = value;
    }

    protected onLoad(): void {
        
    }

    protected onEnable(): void {
        // console.log("onEnable");
        // 重新赋值，对象池取出时
        this.speed = 20;

    }
    protected onDisable(): void {
    }
    start() {
        this.boxCollider2D = this.node.getComponent(BoxCollider2D);
        this.boxCollider2D.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    }

    // 碰撞开始回调函数
    onBeginContact(self: Collider2D, other: Collider2D) {
        this.isMoveing = false;
        Globals.putNode(this.node);
        this.node.active = false;
    }

    update(deltaTime: number) {
        if (this.isMoveing) {
            // 移动
            Util.nodeMove(this.node, this._moveDirection, this.speed);
            // 当速度为0的时候，就销毁此子弹节点
            this.speed -= this._resistance;
            if (this.speed <= 0) {
                // this.node.destroy();
                // 回收
                // NodePoolManager.instance.put(this.node);
                Globals.putNode(this.node);
            }
        }
    }

}


